package com.train.quartz.config;

import com.train.quartz.executor.JobExecutor;
import org.quartz.Scheduler;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.Optional;

@AutoConfiguration
public class JobExecutorConfig {

    /**
     * 如果scheduler没有被启动类扫描,就会为空.
     * @param scheduler
     * @return
     */
    @Bean
    public JobExecutor jobExecutor(Scheduler scheduler) {
        return Optional.ofNullable(scheduler)
                .map(JobExecutor::new)
                .orElseGet(() -> new JobExecutor(null));
    }
}
